package com.tuanbiao.presenter;

import android.text.TextUtils;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.OnLifecycleEvent;

import com.abiao.lib_utils.StringUtils;
import com.hzjq.data.entity.PmEntity;
import com.tuanbiao.AppConfig;
import com.tuanbiao.base.mvp.BasePresenter;
import com.tuanbiao.controllers.ProjectController;
import com.tuanbiao.data.callback.OnBaseResultCallback;
import com.tuanbiao.events.ProjectManageEvent;
import com.tuanbiao.models.ProjectModel;
import com.tuanbiao.utils.IdCardUtil;
import com.tuanbiao.utils.SPHelper;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import timber.log.Timber;

/**
 * @author abiao
 * @since 2022/3/25 16:43
 */
public class ProjectAddPresenter extends BasePresenter<ProjectModel, ProjectController.AddView> implements ProjectController.AddPresenter {
    private PmEntity mProject;

    public ProjectAddPresenter(ProjectModel model, ProjectController.AddView rootView) {
        super(model, rootView);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void init(ProjectManageEvent event) {
        Timber.i(TAG + "#eventbus#init#" + event);
        mProject = event.getEntity();
        EventBus.getDefault().removeStickyEvent(event);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    private void init() {
        Timber.i(TAG + "#init#" + mProject);
        if (null != mProject) mRootView.showDetails(mProject);
        else {
            mRootView.showDetails(SPHelper.getProjectCompanyCode(), SPHelper.isProjectBusinessType());
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    private void inits() {
        if (AppConfig.showTestData()) {
            PmEntity entity = new PmEntity();
            entity.setPmName("测试项目");
            entity.setPmHtId("123456789");
            entity.setPmXmId("1234567");
            entity.setPmCompanyCode("123456");
            entity.setPmManagerCode("522635199508201039");
            entity.setNatureBusiness(true);
            mRootView.showDetails(entity);
        }
    }

    @Override
    public void commit(PmEntity entity) {
        if (checkData(entity)) {
            if (null != mProject) update(entity);
            else insert(entity);
        }
    }

    private void insert(PmEntity entity) {
        mModel.insertProject(entity, new OnBaseResultCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean success) {
                if (success) {
                    SPHelper.setProjectCompanyCode(entity.getPmCompanyCode());
                    SPHelper.setProjectBusinessType(entity.isNatureBusiness());
                    mRootView.showMessage("新增项目成功");
                    mRootView.onResultSuccess();
                } else {
                    mRootView.showMessage("该合同号已存在");
                }

            }

            @Override
            public void onFailed(String msg) {
                mRootView.showMessage(msg);
            }
        });
    }

    private void update(PmEntity entity) {
        mModel.updateProject(entity, success -> {
            if (success) {
                SPHelper.setProjectCompanyCode(entity.getPmCompanyCode());
                SPHelper.setProjectBusinessType(entity.isNatureBusiness());
                mRootView.showMessage("修改项目成功");
                mRootView.onResultSuccess();
            } else {
                mRootView.showMessage("该合同号已存在");
            }
        });
    }

    @Override
    public boolean useEventBus() {
        return true;
    }

    private boolean checkData(PmEntity entity) {
        if (TextUtils.isEmpty(entity.getPmName())) {
            mRootView.showMessage("请输入项目名称");
            return false;
        }
        if (entity.isNatureBusiness() && TextUtils.isEmpty(entity.getPmHtId())) {
            mRootView.showMessage("请输入合同编号");
            return false;
        }
        if (entity.isNatureBusiness() && StringUtils.isContainChinese(entity.getPmHtId())) {
            mRootView.showMessage("合同编号不能包含汉字");
            return false;
        }
        if (entity.isNatureBusiness() && !TextUtils.isEmpty(entity.getPmXmId())) {
            if (StringUtils.isContainChinese(entity.getPmXmId())) {
                mRootView.showMessage("项目编号不能包含汉字");
                return false;
            }
        }

        if (TextUtils.isEmpty(entity.getPmCompanyCode())) {
            mRootView.showMessage("请输入公司编号");
            return false;
        }

        if (StringUtils.isContainChinese(entity.getPmCompanyCode())) {
            mRootView.showMessage("公司编号不能包含汉字");
            return false;
        }

        if (TextUtils.isEmpty(entity.getPmManagerCode())) {
            mRootView.showMessage("请输入爆破员身份证号码");
            return false;
        }
        if (!IdCardUtil.IDCardValidate(entity.getPmManagerCode())) {
            mRootView.showMessage("请输入合法的身份证号码");
            return false;
        }
        return true;
    }

}
